package com.uniqueyi.baselib.utilsbase.sp;

import android.content.Context;
import android.content.SharedPreferences;

/**
 * Created by
 * author:  weiyi.
 * date:    2017/5/16.  10:14.
 * prj:     intercept.
 */
public class SPUtils {

    public static void clearData(Context context) {
        SharedPreferences sp = context.getSharedPreferences("mSpUtils",
                context.MODE_PRIVATE);
        sp.edit().clear().commit();
    }

    public static int getInt(Context context, String k) {
        SharedPreferences sp = context.getSharedPreferences("mSpUtils",
                context.MODE_PRIVATE);
        return sp.getInt(k, 0);
    }

    public static int getInt(Context context, String k, int def) {
        SharedPreferences sp = context.getSharedPreferences("mSpUtils",
                context.MODE_PRIVATE);
        return sp.getInt(k, def);
    }

    public static void setInt(Context context, String k, int i) {
        SharedPreferences sp = context.getSharedPreferences("mSpUtils",
                context.MODE_PRIVATE);
        sp.edit().putInt(k, i).commit();
    }

    public static void setFloat(Context context, String k, float i) {
        SharedPreferences sp = context.getSharedPreferences("mSpUtils",
                context.MODE_PRIVATE);
        sp.edit().putFloat(k, i).commit();
    }

    public static float getFloat(Context context, String k) {
        SharedPreferences sp = context.getSharedPreferences("mSpUtils",
                context.MODE_PRIVATE);
        return sp.getFloat(k, 0);
    }

    public static String getString(Context context, String k) {
        SharedPreferences sp = context.getSharedPreferences("mSpUtils",
                context.MODE_PRIVATE);
        return sp.getString(k, "");
    }

    public static void setString(Context context, String k, String text) {
        SharedPreferences sp = context.getSharedPreferences("mSpUtils",
                context.MODE_PRIVATE);
        sp.edit().putString(k, text).commit();
    }

    public static void setLong(Context context, String key, long value) {
        SharedPreferences sp = context.getSharedPreferences("mSpUtils",
                context.MODE_PRIVATE);
        sp.edit().putLong(key, value).apply();
    }

    public static long getLong(Context context, String key, Long dValue) {
        SharedPreferences sp = context.getSharedPreferences("mSpUtils",
                context.MODE_PRIVATE);
        return sp.getLong(key, dValue);
    }

    public static void setBoolean(Context context, String key, boolean value) {
        SharedPreferences sp = context.getSharedPreferences("mSpUtils",
                context.MODE_PRIVATE);
        sp.edit().putBoolean(key, value).apply();
    }

    public static boolean getBoolean(Context context, String key) {
        return getBoolean(context, key, false);
    }

    public static boolean getBoolean(Context context, String key, boolean dValue) {
        SharedPreferences sp = context.getSharedPreferences("mSpUtils",
                context.MODE_PRIVATE);
        return sp.getBoolean(key, dValue);
    }

    public static void remove(Context context, String key) {
        SharedPreferences sp = context.getSharedPreferences("mSpUtils",
                context.MODE_PRIVATE);
        if (isExist(context, key)) {
            SharedPreferences.Editor editor = sp.edit();
            editor.remove(key);
            editor.apply();
        }
    }

    public static boolean isExist(Context context, String key) {
        SharedPreferences sp = context.getSharedPreferences("mSpUtils",
                context.MODE_PRIVATE);
        return sp.contains(key);
    }
}
